﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GRWL.BZSC.UI.BaseDataSvc;
using GRWL.BZSC.UI.CustomCtrl;

namespace GRWL.BZSC.UI
{
    public partial class EditPrdtPrjtFrm : BaseFrm
    {
        /// <summary>
        /// 当前编码规则
        /// </summary>
        public CodeRuleFrm.CodeRule Rule { get; set; }

        /// <summary>
        /// 传入产品工程系统编号
        /// </summary>
        public int Id { get; set; }

        /// <summary>
        /// 临时变量
        /// </summary>
        public XPrdtPrjt Prj { get; set; }

        /// <summary>
        /// 当前是否编辑模式
        /// </summary>
        public bool EditMode { get; set; }

        /// <summary>
        /// 是否添加子工程
        /// </summary>
        public bool IsSubPrjt { get; set; }

        /// <summary>
        /// 若添加子工程时出入材料Id
        /// </summary>
        public int MaterialId { get; set; }

        public EditPrdtPrjtFrm()
        {
            InitializeComponent();
            this.BaseDataProxy = new BaseDataClient();
        }

        private void EditPrdtPrjtFrm_Load(object sender, EventArgs e)
        {
            this.BaseDataProxy.Open();
            if (this.EditMode)
            {
                this.txtBoxCode.Enabled = false;
                this.Prj = this.BaseDataProxy.GetXPrdtPrjt(this.Id);
                if (this.Prj != null)
                {
                    this.txtBoxAccessor.Text = this.Prj.AssessorName;
                    this.txtBoxCode.Text = this.Prj.PrdtPrjt.code;
                    this.txtBoxCreateTime.Text = this.Prj.PrdtPrjt.createTime.ToString();
                    this.txtBoxHours.Text = this.Prj.PrdtPrjt.hours.ToString();
                    this.txtBoxMaterialName.Text = this.Prj.MaterialName;
                    this.txtBoxSpecs.Text = this.Prj.Specs;
                    this.txtBoxSubmitMan.Text = this.Prj.SubmitManName;
                    this.txtBoxUpdateTime.Text = this.Prj.PrdtPrjt.updateTime.ToString();
                    this.txtBoxVersion.Text = this.Prj.PrdtPrjt.version;

                    if (this.Prj.PrdtPrjt.department != null)
                    {
                        SelectItem item = new SelectItem();
                        item.Name = this.Prj.DepartmentName;
                        item.Key = this.Prj.DepartmentCode;
                        item.Value = this.Prj.PrdtPrjt.department.ToString();
                        this.slcrDepartment.SetSelected(item);
                    }

                    if (this.Prj.PrdtPrjt.engineer != null)
                    {
                        SelectItem item = new SelectItem();
                        item.Name = this.Prj.EngineerName;
                        item.Key = this.Prj.EngineerCode;
                        item.Value = this.Prj.PrdtPrjt.engineer.ToString();
                        this.slcrEngineer.SetSelected(item);
                    }

                    if (this.Prj.PrdtPrjt.material != null)
                    {
                        SelectItem item = new SelectItem();
                        item.Name = this.Prj.MaterialName;
                        item.Key = this.Prj.MaterialCode;
                        item.Value = this.Prj.PrdtPrjt.material.ToString();
                        this.slcrMaterial.SetSelected(item);
                    }
                    this.ckBoxIsRelease.Checked = this.Prj.PrdtPrjt.isRelease;
                    this.ckBoxIsStandard.Checked = this.Prj.PrdtPrjt.isStandard;
                    this.ckBoxIsSubmit.Checked = this.Prj.PrdtPrjt.isSubmit;
                    this.ckBoxIsVerify.Checked = this.Prj.PrdtPrjt.isVerify;

                    setBtnEditable(!this.Prj.PrdtPrjt.isSubmit);
                }
            }
            else
            {
                if (this.Rule != null)
                    this.txtBoxCode.Text = this.BaseDataProxy.GetSerialNo((int)GRWL.BZSC.Common.EnumCommon.CodeRuleType.Other, this.Rule.header, this.Rule.len,!string.IsNullOrEmpty(this.Rule.header), this.Rule.autoFill,true);
                if (this.IsSubPrjt)
                {
                    TB_Material m = this.BaseDataProxy.GetMaterial(this.MaterialId);
                    if (m != null)
                    {
                        this.txtBoxMaterialName.Text = m.cnName;
                        this.txtBoxSpecs.Text = m.specs;

                        SelectItem item = new SelectItem();
                        item.Name = m.cnName;
                        item.Key = m.code;
                        item.Value = m.id.ToString();
                        this.slcrMaterial.SetSelected(item);
                        this.slcrMaterial.Enabled = false;//不能修改
                    }
                }
                this.Text = "新增产品工程";
            }
        }

        /// <summary>
        /// 设置窗体按钮可用状态
        /// </summary>
        /// <param name="enable"></param>
        private void setBtnEditable(bool enable)
        {
            this.btnCopy.Enabled = enable;
            this.btnSave.Enabled = enable;
        }

        private void slcrMaterialCode_SelectorClick(object sender, EventArgs e)
        {
            FindMaterialFrm frm = new FindMaterialFrm();
            frm.StockNullable = true;
            frm.IsPurchase = false;
            if (frm.ShowDialog() == DialogResult.OK)
            {
                if (frm.SelectorItem != null)
                {
                    SelectItem item = new SelectItem();
                    item.Key = frm.SelectorItem.Code;
                    item.Name = frm.SelectorItem.CnName;
                    item.Value = frm.SelectorItem.Id.ToString();
                    this.slcrMaterial.SetSelected(item);
                    this.txtBoxMaterialName.Text = frm.SelectorItem.CnName;
                    this.txtBoxSpecs.Text = frm.SelectorItem.Specs;
                }
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (this.txtBoxCode.Text == string.Empty)
            {
                MessageBox.Show("请输入工程编号！");
                this.txtBoxCode.Focus();
                return;
            }

            if (this.slcrMaterial.CurrentSelectItem == null)
            {
                MessageBox.Show("请选择物料！");
                this.slcrMaterial.Focus();
                return;
            }

            XPrdtPrjt xPrj = new XPrdtPrjt();
            xPrj.PrdtPrjt = new TB_ProductProject();

            if (this.slcrDepartment.SelectedValue() != null)
                xPrj.PrdtPrjt.department = Convert.ToInt32(this.slcrDepartment.SelectedValue());
            if (this.slcrEngineer.SelectedValue() != null)
                xPrj.PrdtPrjt.engineer = Convert.ToInt32(this.slcrEngineer.SelectedValue());
            if (!string.IsNullOrEmpty(this.txtBoxHours.Text))
                xPrj.PrdtPrjt.hours = Convert.ToInt32(this.txtBoxHours.Text.Trim());
            xPrj.PrdtPrjt.isRelease = this.ckBoxIsRelease.Checked;
            xPrj.PrdtPrjt.isStandard = this.ckBoxIsStandard.Checked;
            if (this.slcrMaterial.CurrentSelectItem != null)
            {
                xPrj.PrdtPrjt.material = Convert.ToInt32(this.slcrMaterial.SelectedValue());
                xPrj.PrdtPrjt.materialCode = this.slcrMaterial.CurrentSelectItem.Key;
                xPrj.MaterialCode = this.slcrMaterial.CurrentSelectItem.Key;
                xPrj.MaterialName = this.slcrMaterial.CurrentSelectItem.Name;
                xPrj.Specs = this.txtBoxSpecs.Text;
            }
            xPrj.PrdtPrjt.version = this.txtBoxVersion.Text;

            bool ok = false;
            Common.OperateType opType = Common.OperateType.Add;
            if (this.EditMode)
            {
                xPrj.PrdtPrjt.id = this.Prj.PrdtPrjt.id;
                xPrj.PrdtPrjt.assessor = this.Prj.PrdtPrjt.assessor;
                xPrj.PrdtPrjt.code = this.Prj.PrdtPrjt.code;
                xPrj.PrdtPrjt.createTime = this.Prj.PrdtPrjt.createTime;
                xPrj.PrdtPrjt.deleteFlag = this.Prj.PrdtPrjt.deleteFlag;
                xPrj.PrdtPrjt.isSubmit = this.Prj.PrdtPrjt.isSubmit;
                xPrj.PrdtPrjt.isVerify = this.Prj.PrdtPrjt.isVerify;
                xPrj.PrdtPrjt.submitMan = this.Prj.PrdtPrjt.submitMan;
                xPrj.PrdtPrjt.submitStatus = this.Prj.PrdtPrjt.submitStatus;
                xPrj.PrdtPrjt.updateTime = DateTime.Now;
                xPrj.PrdtPrjt.verifyStatus = this.Prj.PrdtPrjt.verifyStatus;
                xPrj.PrdtPrjt.version = this.Prj.PrdtPrjt.version;
                ok = this.BaseDataProxy.UpdatePrdtPrjt(xPrj.PrdtPrjt);
                opType = Common.OperateType.Update;
            }
            else
            {
                xPrj.PrdtPrjt.submitMan = SysLoginUserInfo.UserId;
                xPrj.PrdtPrjt.version = this.BaseDataProxy.GetSerialNo((int)GRWL.BZSC.Common.EnumCommon.CodeRuleType.Other, "V", 2,true, true,false);
                //xPrj.PrdtPrjt.code = this.BaseDataProxy.GetSerialNo((int)GRWL.BZSC.Common.EnumCommon.CodeRuleType.Other, this.Rule.header, this.Rule.len, this.Rule.autoFill);
                xPrj.PrdtPrjt.code = this.txtBoxCode.Text;
                xPrj.PrdtPrjt.createTime = DateTime.Now;
                int newId = this.BaseDataProxy.AddPrdtPrjt(xPrj.PrdtPrjt);
                xPrj.PrdtPrjt.id = newId;
                ok = newId > 0;
            }
            if (ok)
            {
                CommonEventArg<XPrdtPrjt> arg = new CommonEventArg<XPrdtPrjt>(xPrj, opType);
                if (this.IsSubPrjt)
                    GRWL.BZSC.Common.EventCommon.OnSubPrdtPrjtUdTVEvnt(sender, arg);
                else
                    GRWL.BZSC.Common.EventCommon.OnPrdtPrjtUdTVEvnt(sender, arg);

            }
            this.Close();
        }

        private void slcrDepartment_SelectorClick(object sender, EventArgs e)
        {
            DeptFrm frm = new DeptFrm();
            if (frm.ShowDialog() == DialogResult.OK)
                this.slcrDepartment.SetSelected(frm.SelectorItem);
        }

        private void slcrEngineer_SelectorClick(object sender, EventArgs e)
        {
            UserFrm frm = new UserFrm();
            if (frm.ShowDialog() == DialogResult.OK)
                this.slcrEngineer.SetSelected(frm.SelectorItem);
        }

        private void btnCopy_Click(object sender, EventArgs e)
        {
            if (this.EditMode == false)
            {
                MessageBox.Show("请先保存工程！");
                return;
            }
            if (MessageBox.Show("工程复制后新的树结构会取代旧的工程树结构，确定复制吗？", "复制提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                FindPrdtPrjtFrm frm = new FindPrdtPrjtFrm(string.Empty);
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    if (frm.SelectorItem != null)
                    {
                        //1、清除原工程结构 2、复制目标结构，3、加载前端树结构
                        if (this.BaseDataProxy.CopyPrdtPrjt(frm.SelectorItem.Id, this.Id))
                        {
                            CommonEventArg<Int32> arg = new CommonEventArg<Int32>(this.Id, Common.OperateType.Update);
                            GRWL.BZSC.Common.EventCommon.OnCopyPrjtUdTVEvnt(sender, arg);
                        }
                    }
                }
            }
        }

        private void slcrMaterial_SelectorTextUpdate(object sender, EventArgs e)
        {
            string key =this.slcrMaterial.GetComboBoxText();
            QMaterial[] qms = this.BaseDataProxy.FindMaterials(null, null, null, null, null, key, null, null, false, null);
            this.slcrMaterial.ReBindComboBox<QMaterial>(qms, "Id", "CnName", "Code");
        }

        private void slcrDepartment_SelectorTextUpdate(object sender, EventArgs e)
        {
            string key = this.slcrDepartment.GetComboBoxText();
            TB_Department[] depts = this.BaseDataProxy.FindDepartments(null, null, key);
            this.slcrDepartment.ReBindComboBox<TB_Department>(depts, "id", "name", "code");
        }

        private void slcrEngineer_SelectorTextUpdate(object sender, EventArgs e)
        {
        }
    }
}
